<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="<%=basePath%>resources/css/bootstrap.min.css" rel="stylesheet"/>
    <title>项目脚手架</title><!--TODO project-->

    <style type="text/css">
        .container {
            margin: 30px auto 0;
            width: 700px;
        }
        .tweetBtn {
            margin-top: 10px;
            width: 100px;
        }
        .inputHint {
            margin: 25px 20px 0;
        }

        .myTweets h4 {
            margin-top: 50px;
        }
        .tweet {
            border: 1px solid #e8e8e8;
            border-radius: 4px;
            margin-bottom: 15px;
        }
        .content {
            padding: 5px;
        }
        .count {
            border-top: 1px solid #e8e8e8;
            margin: 0;
        }
        .comment {
            display: inline-block;
            margin: 5px 10px;
        }
        .delBtn {
            padding: 3px 20px;
            font-size: 0.2em;
            margin: 2px 4px;
        }
    </style>
</head>
<body>

<div class="container">
    <form class="form-tweet">
        <textarea class="form-control newContent" rows="5" required="" autofocus="" onautocomplete=""></textarea>
        <button class="btn btn-primary pull-right tweetBtn" type="submit">发送</button>
        <span class="pull-right inputHint">还可以输入 <b>140</b> 个字</span>
    </form>

    <div class="myTweets">
        <h4>我的Tweet</h4>
        <hr>
        <div class="tweetList">
        </div>
    </div>
</div> <!-- /container -->

<script src="<%=basePath%>resources/js/jquery-2.1.0.min.js"></script>
<script src="<%=basePath%>resources/js/bootstrap.min.js"></script>
<script>
$(function () {
    (function loadAllTweet() {
        $.ajax({
            type: "GET",
            contentType: "application/json",
            url: "<%=basePath%>tweet/all",
            dataType: 'json'
        })
        .done(function (resp) {
            $.each(JSON.parse(resp), function (i, item) {
                appendTweet(item.content, item.id);
            });
        })
        .fail(function (e) {
            alert("请求失败!");
        });
    })();

    $(".tweetBtn").click(function (e) {
        $(".tweetBtn").attr("disabled", true);
        e.preventDefault();
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "<%=basePath%>tweet/publish",
            data: JSON.stringify({
                content: $(".newContent").val()
            }),
            dataType: 'json'
        })
        .done(function (resp) {
            location.reload();
        })
        .fail(function (e) {
            alert("请求失败!");
        });
    });

    function appendTweet(content, id) {
        var tmpl =
                '<div class="tweet"><p class="content">{content}</p><p class="count"><span class="comment">评论 <a href="javascript:void (0)">0</a></span>' +
                '<button class="btn btn-danger pull-right delBtn" type="submit" data-id="{id}">删除</button></p></div>';
        $(".tweetlist").prepend(tmpl.replace("{content}", content).replace("{id}", id));
    }

    $(".tweetList").delegate(".delBtn", "click", function (e) {
        var tweet = $(this).parents(".tweet");

        e.preventDefault();
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "./delete/" + $(this).data("id")
        })
        .done(function (resp) {
            tweet.remove();
        })
        .fail(function (e) {
            alert("请求失败!");
        });
    });
});
</script>
</body>
</html>
